﻿using System;
using System.IO;
using System.Web;
using Castle.ActiveRecord;
using Castle.Components.Scheduler.JobStores;
using Castle.Components.Scheduler.WindsorExtension;
using Castle.Core.Resource;
using Castle.MicroKernel.Registration;
using Castle.MonoRail.Framework.Configuration;
using Castle.MonoRail.Framework.Internal;
using Castle.MonoRail.Framework.Views.NVelocity;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.MonoRail.Framework;
using Castle.MonoRail.WindsorExtension;
using Castle.Facilities.AutomaticTransactionManagement;
using Castle.Facilities.ActiveRecordIntegration;
using Castle.Components.Scheduler;
using DocumentaWeb.Controllers;
using DocumentaWeb.Core.Mappers;
using DocumentaWeb.Core.Services;
using DocumentaWeb.Core;
using DocumentaWeb.Core.Domain;
using DocumentaWeb.Core.Repositories;


namespace DocumentaWeb.UI
{
	public class Global : HttpApplication, IMonoRailConfigurationEvents, IContainerAccessor
	{
		private static WindsorContainer container;

		public IWindsorContainer Container
		{
			get { return container; }
		}

		public void Configure(IMonoRailConfiguration configuration)
		{
			configuration.ViewEngineConfig.ViewPathRoot = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Views");
			configuration.ViewEngineConfig.ViewEngines.Add(new ViewEngineInfo(typeof (NVelocityViewEngine),false));
		}

		protected void Application_Start(object sender, EventArgs e)
		{
			container = new WindsorContainer(new XmlInterpreter(new ConfigResource()));

			/// declaracion de facilities
			container.AddFacility("mr", new MonoRailFacility());
			container.AddFacility("atm", new TransactionFacility());
			container.AddFacility("ar", new ActiveRecordFacility());

			/// declaracion de componentes
			/// 
			/// Este primero es el trabajo corriendo en backgroud
			container.Register(Component.For<IScheduler>().Named("Core.Scheduling.Scheduler").ImplementedBy<DefaultScheduler>());
			container.Register(Component.For<IJobStore>().ImplementedBy<SqlServerJobStore>().Parameters(Parameter.ForKey("connectionString").Eq("Server=(local);Initial Catalog=schedulerTestDb;Integrated Security=SSPI")));
			container.Register(Component.For<IJobRunner>().ImplementedBy<DefaultJobRunner>());
			container.Register(Component.For<IJobFactory>().ImplementedBy<WindsorJobFactory>());
			container.Register(Component.For<IJob>().Named("Trabajo").ImplementedBy<Trabajo>());

			/// declaracion de domain
			container.Register(Component.For<IContratista>().ImplementedBy<Contratista>());


			
			/// declaracion de controladores
			container.Register(Component.For<HomeController>());
			container.Register(Component.For<ContratistaController>());
			
			

			/// declaración de servicios
			container.Register(Component.For<IContratistaService>().ImplementedBy<ContratistaService>().LifeStyle.Transient);


			/// declaracion de mappers
			container.Register(Component.For<IContratistaMapper>().ImplementedBy<ContratistaMapper>());

			/// declaración de repositorios
			 container.Register(
			 Component.For<IContratistaRepository>().ImplementedBy<ContratistaRepository>().LifeStyle.Transient);

			 ActiveRecordStarter.CreateSchema();
			 IScheduler scheduler = (IScheduler)container["Core.Scheduling.Scheduler"];
			 Trigger trigger = new PeriodicTrigger(new DateTime(1900, 1, 1), new DateTime(2900, 1, 1), new TimeSpan(0, 0, 10), 1000);
			 JobSpec jobSpec = new JobSpec("My job.", "A test job.", "Trabajo", trigger);
			 scheduler.CreateJob(jobSpec, CreateJobConflictAction.Ignore);
			 scheduler.Start();
		}


		protected void Application_End(object sender, EventArgs e)
		{
			container.Dispose();
			ActiveRecordStarter.DropSchema();

		}
	}
}